package com.alks.function.mapper.product;

import com.alks.entity.data.entity.*;
import com.alks.function.data.request.stock.PcOutMaterialReq;
import com.alks.function.data.request.stock.PcSlipTempPartOReq;
import com.alks.function.data.request.stock.PcSlipTempPartReq;
import com.alks.function.data.request.stockdept.pcfactorystockwork.CsmDetRequest;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Mapper
public interface SdMaterialMapper {

    @Select("SELECT * FROM PC_SLIP_TEMP_PART WHERE SYS_USER = #{username} AND SLIP_TYPE = 'I'")
    List<PcSlipTempPartBf> sdMaterialList(String username);

    void insertMaterialPart(@Param("mtlNo") String mtlNo, @Param("username") String username, @Param("max") Integer max);

    Integer getPcSlipTempPartMax(@Param("loginName") String loginName);

    void insertBatchPcPart(@Param("req") PcSlipTempPartReq req, @Param("slipNo") String slipNo, @Param("loginName") String loginName);

    @Select("SELECT COUNT(*) FROM PC_SLIP_TEMP_PART WHERE MTL_NO = #{mtlNo} AND SYS_USER = #{username} AND SLIP_TYPE = 'I'")
    Integer selectByMtlNoI(@Param("mtlNo") String mtlNo, @Param("username") String username);

    @Select("SELECT COUNT(*) FROM PC_SLIP_TEMP_PART WHERE MTL_NO = #{mtlNo} AND SYS_USER = #{username} AND SLIP_TYPE = 'O'")
    Integer selectByMtlNoO(@Param("mtlNo") String mtlNo, @Param("username") String username);

    @Update("UPDATE PC_SLIP_TEMP_PART SET QTY = #{req.qty},REMARK = NULLIF(#{req.remark},'') WHERE MTL_NO = #{req.mtlNo} AND SLIP_TYPE = #{choose} ")
    void updatePcSlipTempPart(@Param("req") PcSlipTempPartResp req, @Param("choose") String choose);

    @Delete("DELETE FROM PC_SLIP_TEMP_PART WHERE SYS_USER = #{loginName} AND SLIP_TYPE = 'I'")
    void deleteTempPartFromUser(String loginName);

    List<PcSlipDetailPart> pcSlipDetailPartList(@Param("req") CsmDetRequest req, @Param("username") String username);

    @Delete("DELETE  FROM PC_SLIP_TEMP_PART WHERE SYS_USER = #{loginName} AND MTL_NO = #{mtlNo}")
    void deletePcSlipDetailPart(@Param("mtlNo") String mtlNo, @Param("loginName") String loginName);

    @Select("SELECT STK_QTY FROM PC_MTL_ACC_PART WHERE MTL_NO = #{string}")
    BigDecimal getByPcMtlAcc(String string);

    void insertIntoAccPart(@Param("string") String string);

    @Select("SELECT QTY FROM PC_SLIP_TEMP_PART WHERE MTL_NO = #{string}")
    BigDecimal getTempPartQty(String string);

    @Update("UPDATE PC_MTL_ACC_PART SET IN_QTY = #{acc.inQty},STK_QTY = #{acc.stkQty},REC_DATE = now() WHERE MTL_NO = #{string}")
    void updateAccPart(@Param("acc") PcMtlAccPack acc, @Param("string") String string);

    List<Map<String, Object>> outMaterialList(@Param("typeNo") String typeNo, @Param("typeName") String typeName, @Param("mtlName") String mtlName);

    void insertIntoOutTemp(@Param("mtlNo") String mtlNo, @Param("name") String name, @Param("max") Integer max);

    @Select("SELECT  IFNULL(CAST(p1.STK_QTY AS DECIMAL(10,2)),'') stkQty" +
            "        FROM SD_MATERIAL_PART s1\n" +
            "        LEFT JOIN\n" +
            "        PC_MTL_ACC_PART p1\n" +
            "        ON s1.MTL_NO = p1.MTL_NO" +
            " WHERE s1.MTL_NO = #{mtlNo}")
    BigDecimal getStockByacc(String mtlNo);

    @Select(" SELECT MAX(ITEM) FROM PC_SLIP_TEMP_PART WHERE SYS_USER = #{loginName} AND SLIP_TYPE = 'O'")
    Integer getPcSlipTempPartOMax(String loginName);

    List<PcSlipTempPart> pcSlipTempPartOutList(String loginName);

    @Select("SELECT OUT_QTY FROM PC_MTL_ACC_PART WHERE MTL_NO = #{mtlNo}")
    BigDecimal getOutQty(String mtlNo);

    void subtractPcSlipDetailPart(@Param("num") BigDecimal num, @Param("mtlNo") String mtlNo);

    void insertIntoOutDetail(@Param("req") PcSlipTempPartOReq req, @Param("name") String name, @Param("slipNo") String slipNo);

    @Delete("DELETE FROM PC_SLIP_TEMP_PART WHERE SLIP_TYPE = 'O' AND SYS_USER = #{name}")
    void deletePcSlipTempO(String name);

    @Delete("DELETE FROM PC_SLIP_TEMP_PART WHERE SLIP_TYPE = 'O' AND MTL_NO = #{mtlNo} AND SYS_USER = #{name}")
    void deletePcSlipTemp(@Param("mtlNo") String mtlNo, @Param("name") String name);

    List<PcSlipDetailPart> pcSlipDetailOutList(@Param("req") PcOutMaterialReq req, @Param("name") String name);

    @Delete("DELETE FROM PC_SLIP_DETAIL_PART WHERE SYS_USER = #{loginName} AND SLIP_NO = #{slipNo} AND MTL_NO = #{mtlNo} AND SLIP_TYPE = 'O'")
    void deletePcSlipDetailPartO(@Param("slipNo") String slipNo, @Param("mtlNo") String mtlNo, @Param("loginName") String loginName);

    BigDecimal getPcDetailQty(@Param("mtlNo") String mtlNo, @Param("slipNo") String slipNo, @Param("name") String name);

    void updatePcAcc(@Param("mtlNo") String mtlNo, @Param("outQty") BigDecimal outQty, @Param("stkQty") BigDecimal stkQty);

}
